//
//  YZAddController.m
//  kuaiyi
//
//  Created by wyman on 16/5/4.
//
//

#import "YZAddController.h"
#import "YZCaseTableViewCell.h"
#import "YZNetWorkTool.h"
#import "YZManagerModels.h"
#import "YZillTypeTableViewController.h"
#import <Masonry.h>
#import <MBProgressHUD.h>
#import <SVProgressHUD.h>
#import "YZillMessage.h"
#import "YZSegmentController.h"


@interface YZAddController ()

//创建数组

@property (nonatomic, strong) NSMutableArray *describelArray;

@property (nonatomic, strong) NSArray *dates;
@property (nonatomic, strong) NSArray *illType;

@property (nonatomic, strong) UIImagePickerController *pikerContrller;

//疾病类型
@property (nonatomic, copy) NSString *tyoeName;
//疾病细分
@property (nonatomic, copy) NSString *segmentName;

@property (nonatomic, strong) UIImage *selectedImage;

@property (nonatomic, copy) NSString *describleLabel;
@end

@implementation YZAddController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [SVProgressHUD setMinimumDismissTimeInterval:0.5];
    self.illType = @[@"肿瘤",@"血液病",@"心脑血管病",@"神经系统",@"骨科病"];
    self.tyoeName = @"请选择";
    self.segmentName = @"请选择";
    self.selectedImage = [UIImage imageNamed:@"compose_pic_add_highlighted"];


}

  //MARK - 网络请求数据

-(void)getNetDates{
    
    NSString *urlString = @"http://iosapi.itcast.cn/doctor/searchCI3List.json.php";
    NSDictionary *parameters = @{
                                 @"page":@1,
                                 @"page_size":@15,
                                 @"ci1_id":@1,
                                 @"keyword":@"",
                                 };
    
    [[YZNetWorkTool sharedNetWorkTool]POST:urlString parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *responseObject) {
        
        NSArray *dateArr = responseObject[@"data"];
        
        NSArray *dates = [YZManagerModels arrWithArray:dateArr];
        
        self.dates = dates;
        NSLog(@"%@",dateArr);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return 2;

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return section==0 ? 2 : 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *ID = [YZCaseTableViewCell cellIDWithNew:indexPath.section];
    
    YZCaseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID forIndexPath:indexPath];
    if (indexPath.section == 0) {
        //TODO:去掉分割线,加一个view
        
        if (indexPath.row == 0) {
            cell.typeName = self.tyoeName;
        }else if (indexPath.row ==1){
            cell.typeName = self.segmentName;
        }
      
        
    }
    
    if (indexPath.section == 1) {
        
        cell.seletedImage = self.selectedImage;

        
    }
    
 
    __weak YZAddController *weakself = self;

    cell.illdesTypeblock = ^(NSString *illdes){
        weakself.describleLabel = illdes;
        
    };
    cell.addDidBlock = ^(){
    
        [weakself presentViewController:weakself.pikerContrller animated:YES completion:nil];

    };
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    return [YZCaseTableViewCell cellWithHeight:indexPath.section];
}

/**
 *  实现点击方法
 *
 */
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 1) {
        return;
    }
    if (indexPath.row == 0) {
        YZillTypeTableViewController *Ync = [[YZillTypeTableViewController alloc]init];
        [self.navigationController pushViewController:Ync animated:YES];
        
        Ync.illTypeblock = ^(NSString *typeName){

            self.tyoeName = typeName;
            [self.tableView reloadData];
        };
    }
    if (indexPath.row == 1) {
       
        if ([self.tyoeName isEqualToString:@"请选择"]) {
            [SVProgressHUD showErrorWithStatus:@"请填写疾病种类"];

            return;
        }
        
        
        //MARK: 什么类型对应什么病
        
        YZSegmentController *Ync = [[YZSegmentController alloc]init];
        
        [self.illType enumerateObjectsUsingBlock:^(NSString *obj, NSUInteger idx, BOOL * _Nonnull stop) {
            
            if ( [self.tyoeName isEqualToString:obj]){
                Ync.cil_id = @(idx+1);
            }
        }];
        

        [self.navigationController pushViewController:Ync animated:YES];
        
        Ync.illTypeblock = ^(NSString *typeName){
            
            self.segmentName = typeName;
            [self.tableView reloadData];
        };
    }
    
}

//懒加载



//返回组的高度
//-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
//    return 20;
//}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 20;
}

//- (IBAction)backButton:(UIBarButtonItem *)sender {
//
//
//    
//    [self.navigationController popViewControllerAnimated:YES];
// 
//}


-(void)text{



}

-(UIImagePickerController *)pikerContrller{

    if (_pikerContrller == nil) {
        _pikerContrller = [[UIImagePickerController alloc] init];
        _pikerContrller.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        _pikerContrller.delegate = self;

    }
    
    return _pikerContrller;
}

#pragma mark :- 代理协议

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{

    self.selectedImage = info[UIImagePickerControllerOriginalImage];
    [self.pikerContrller dismissViewControllerAnimated:YES completion:nil];
    [self.tableView reloadData];
}


#pragma mark: 提交方法

- (IBAction)tijiao:(UIBarButtonItem *)sender {
    

    NSDate *date = [NSDate date];
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    NSString *timeLabel = [formatter stringFromDate:date];
    
    YZillMessage *ills = [[YZillMessage alloc]init];
    
    
    ills.illType = [NSString stringWithFormat:@"%@   %@",self.tyoeName,self.segmentName];
    ills.photoImage = self.selectedImage;
    ills.describleLabel = self.describleLabel;
    ills.timeLabel = timeLabel;
    NSDictionary *dic = @{@"ills":ills};
    
    
    //发送通知
    
//    self.illMessageblock(ills);
    [[NSNotificationCenter defaultCenter] postNotificationName:@"shownotice" object:self userInfo:dic];
    
    if ([self.delegate respondsToSelector:@selector(addConDidClick:illinfo:)]) {
        [self.delegate addConDidClick:self illinfo:ills];
    }
    
    if ([self.tyoeName isEqualToString: @"请选择"]) {
        
        [SVProgressHUD showErrorWithStatus:@"请填写疾病类型"];

        return;
    }
    if (self.describleLabel.length == 0) {
        
        [SVProgressHUD showErrorWithStatus:@"请填写疾病描述"];
        
        return;
    }
    

    [self.navigationController popViewControllerAnimated:YES];
    
}



-(NSMutableArray *)describelArray{
    if (_describelArray == nil) {
        _describelArray = [NSMutableArray array];
    }
    return _describelArray;

}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{

    [self.tableView endEditing:YES];
}


@end
